<?php	
	include('db_connection.php');
		if(isset($_POST['pay_type_status'])){
			$return_type = '';
			$select_usr_dtl = "SELECT * FROM tbl_user_details WHERE id='".$_POST['userdtlpayerId']."'";
			$query_usr_dtl = mysql_query($select_usr_dtl) or die(mysql_error());

			if(mysql_num_rows($query_usr_dtl)>0){
				while($row_usr_dtl = mysql_fetch_array($query_usr_dtl)){
					$course_id = $row_usr_dtl['course_id'];
					$course_fee = '';
					$course_pwd = '';

					if($row_usr_dtl['is_ebook'] == 1){
						$sel_course_fee = "SELECT * FROM tbl_ebook where id = '".$course_id."'";
						$query_course_fee = mysql_query($sel_course_fee) or die(mysql_error());
						if(mysql_num_rows($query_usr_dtl)>0){
							while($row_course_fee = mysql_fetch_array($query_course_fee)){
								$course_fee = $row_course_fee['ebook_fee'];
								$course_pwd = $row_course_fee['ebook_pwd'];
							}
						}
					}else{
						$sel_course_fee = "SELECT * FROM tbl_courses where id = '".$course_id."'";
						$query_course_fee = mysql_query($sel_course_fee) or die(mysql_error());
						if(mysql_num_rows($query_usr_dtl)>0){
							while($row_course_fee = mysql_fetch_array($query_course_fee)){
								$course_fee = $row_course_fee['course_fee'];
							}
						}
					}					
					
					
					$user_id = $row_usr_dtl['user_id'];
					$insert_cart = "INSERT INTO tbl_cart_payment (user_id,course_id,user_detail_id,payment) values('".$row_usr_dtl['user_id']."','".$course_id."','".$_POST['userdtlpayerId']."','".$course_fee."')";
					$query_insert_cart = mysql_query($insert_cart) or die(mysql_error());

					$chk_dr_val = '';
					if(isset($_POST['chq_drft_val'])){
						if($_POST['chq_drft_val']!=''){
							$chk_dr_val = $_POST['chq_drft_val'];
						}					
					}

					$sel_bank_status = "SELECT * FROM tbl_bank_status WHERE id='".$_POST['pay_type_status']."'";
					$course_bank_status = mysql_query($sel_bank_status) or die(mysql_error());
					if(mysql_num_rows($course_bank_status)>0){
						while($row_bank_status = mysql_fetch_array($course_bank_status)){
							$return_type[] = $_POST['pay_type_status'];
							$update_bak_status  = "UPDATE tbl_user_details SET via_paid='".$_POST['pay_type_status']."',cheque_draft_no='".$chk_dr_val."' WHERE id='".$_POST['userdtlpayerId']."'";
							$query_bank_status = mysql_query($update_bak_status) or die(mysql_error());
						}
					}

					$update = "UPDATE tbl_user SET is_paid='1', is_active='1', update_ts=now() WHERE id='".$row_usr_dtl['user_id']."'";
					$user_update = mysql_query($update) or die(mysql_error());
					$msg = "Bank Detail Updated Successfully";

					$select_user = "SELECT a.*,e.ebook_name,c.course_name,b.is_ebook,b.course_fee as user_course_fee,b.phone as phone_number,b.create_ts as detail_create,b.id as user_detail_id FROM tbl_user a
                                            Left JOIN tbl_user_details b ON a.id = b.user_id 
                                            Left JOIN tbl_courses c ON c.id = b.course_id 
                                            Left JOIN tbl_ebook e ON e.id = b.course_id 
                                            WHERE a.id='".$row_usr_dtl['user_id']."'";		
					$query_user = mysql_query($select_user) or die(mysql_error());
					$count_row_user = mysql_num_rows($query_user);
					$email = '';
					$pwd = '';
					
					if($count_row_user>0){
						while($row = mysql_fetch_array($query_user)){
							$email = $row['email'];
                                                        $username = $row['username'];
                                                        $phone_number = $row['phone_number'];
                                                        $user_detail_id = $row['user_detail_id'];
                                                        $detail_create = $row['detail_create'];
                                                        $user_course_fee = $row['user_course_fee'];
                                                        if($row['is_ebook'] == 1){
                                                            $course_name = $row['ebook_name'];
                                                        }else{
                                                            $course_name = $row['course_name'];
                                                        }
                                                        
							$refrence_url = $row['reference_url'];
							$pwd = $row['password'];
						}
					}
					if($row_usr_dtl['is_ebook'])
                                            {
                                            
						$mail_to=$email ;
						$mail_subject="Download Ebook Link";
						$mail_body.="Click the following link to download your ebook.<br/>\n";
						$mail_body.="Password = ".$course_pwd.".<br/>\n";
						$mail_body.="<a href='http://rvtraining.net/download_ebook.php?download_link=".$refrence_url.".".$row_usr_dtl['id']."'>Click here</a>";
						$headers  = 'MIME-Version: 1.0' . "\r\n";
						$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
						$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
						$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
						
						$mail_to='Vipin7477@gmail.com' ;
						$mail_subject="Download Ebook Link";
						$mail_body.="Click the following link to download your ebook.<br/>\n";
						$mail_body.="Password = ".$course_pwd.".<br/>\n";
						$mail_body.="<a href='http://rvtraining.net/download_ebook.php?download_link=".$refrence_url.".".$row_usr_dtl['id']."'>Click here</a>";
						$headers  = 'MIME-Version: 1.0' . "\r\n";
						$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
						$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
						$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
					}
                                        else{
						if($pwd==''){
                                                        #echo 'here4';exit;
							$random_string_final = '';
							$pwd_row = '';
							$alpha_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
							$numeric_arr = array('0','1','2','3','4','5','6','7','8','9');
							
							function array_random_assoc($arr, $num = 1) {
								$keys = array_keys($arr);
								shuffle($keys);
								
								$r = '';
								for ($i = 0; $i < $num; $i++) {
									$r .= $arr[$keys[$i]];
								}
								return $r;
							}
							$count = 1;
							while($count<2)
							{
                                                            
								$random_alpha_string = array_random_assoc($alpha_arr,4);
								$random_numeric_string = array_random_assoc($numeric_arr,4);
								$random_string = $random_alpha_string.$random_numeric_string;
								$random_string = str_shuffle($random_string);		
								

								$select = "SELECT password FROM tbl_user WHERE password='".md5($random_string)."'";
								
								$query = mysql_query($select);
								$row = mysql_num_rows ( $query );
								if(!$row){					
									$count = 2;
									$random_string_final = $random_string;
								}
							}

							if($count == 2){
                                                           
								$update = "UPDATE tbl_user SET password='".md5($random_string_final)."' WHERE id='".$row_usr_dtl['user_id']."'";
								$user_update = mysql_query($update) or die(mysql_error());

                                                          
                                                                      
                                                                        
                                                                        
								$mail_to=$email;
								$mail_subject="User Name And Password";
								//$mail_body = "This is the email for forwarding email And Password.<br/>\r\n";
								//$mail_body.="Login URL = <a href='http://rvtraining.net/traineeAdmin/'>http://rvtraining.net/traineeAdmin</a> <br/>\r\n";
								//$mail_body.="User Name = ".$email."<br/>\r\n";
								//$mail_body.="Password = ".$random_string_final."<br/>\r\n";
								$headers  = 'MIME-Version: 1.0' . "\r\n";
								$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
								$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
								$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
								
								$mail_to='Vipin7477@gmail.com' ;
								$mail_subject="User Name And Password";
								//$mail_body = "This is the email for forwarding email And Password.<br/>\r\n";
								//$mail_body.="Login URL = <a href='http://rvtraining.net/traineeAdmin/'>http://rvtraining.net/traineeAdmin</a> <br/>\r\n";
								//$mail_body.="User Name = ".$email."<br/>\r\n";
								//$mail_body.="Password = ".$random_string_final."<br/>\r\n";
								$headers  = 'MIME-Version: 1.0' . "\r\n";
								$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
								$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
								$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
							}
						}
					
                                                
                                        }
                                        $student_name = $username;
                                        $student_email = $email;
                                        $student_number = $phone_number;
                                        $invoice_id = "RV".$user_detail_id;
                                        $invoice_date = date('Y-m-d',strtotime($detail_create));
                                        $course_name = $course_name;
                                        $course_qty = "1";
                                        $course_price = "$".$user_course_fee;
                                        $total_price = "$".$user_course_fee;
                                                                
                                                               
                                                                
                                                                
                                                                ob_start();
                                                                include_once('../../mailtemplate/invoice.php');
                                                                $mail_body = ob_get_contents();
                                                                ob_end_clean();  
                                                                $mail_to=$email;
								$mail_subject="Invoice";
                                                                $headers  = 'MIME-Version: 1.0' . "\r\n";
								$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
								$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
								$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
								
                                                                
                                                                $mail_to= 'Vipin7477@gmail.com' ;
								$mail_subject="Invoice";
                                                                $headers  = 'MIME-Version: 1.0' . "\r\n";
								$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
								$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
								$sent = mail($mail_to,$mail_subject,$mail_body, $headers);
								
                                                                
                                                                $username = $email;
                                                                $password = $random_string_final;
                                                                ob_start();
                                                                include_once('../../mailtemplate/trainee_login.php');
                                                                $mail_body = ob_get_contents();
                                                                ob_end_clean();  
					echo json_encode($return_type);

				}
			}

		}
?>